<template>
  <div class="right_bottom_inner">
    <el-form label-position="right" :rules="rules" label-width="100px" :model="form" style="width: 70%;" ref="rightBottomRuleForm">
      <el-form-item label="通道:" prop="channel">
        <el-select v-model="form.channel" placeholder="请选择">
          <el-option
              v-for="item in channelOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="自定义命令:" prop="cmd">
        <el-input v-model="form.cmd" placeholder="请输入命令"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('rightBottomRuleForm')">
          立即发送
          <svg class="iconfont" aria-hidden="true" style="color: #fff">
            <use xlink:href="#icon-mti-yun-fasongzhiling"></use>
          </svg>
        </el-button>
        <el-button @click="resetForm('rightBottomRuleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { currentGET } from "api/modules";
import {graphic} from "echarts"
import dataValues from "@/assets/js/data_values.json"
import moment from "moment/moment";
import axios from "axios";

export default {
  data() {
    return {
      channelOptions:[],
      form:{
        channel:null,
        cmd:null
      },
      rules: {
        channel: [
          {required: true, message: '请选择通道', trigger: 'blur'},
        ],
        cmd: [
          {required: true, message: '请输入命令', trigger: 'blur'}
        ],
      }
    };
  },
  watch:{
    '$store.state.setting.channelOptionStore':{
      handler () {
        this.getData();
      },
      deep: true
    }
  },
  created() {

  },
  mounted() {
    this.getData();
  },
  methods: {
    submitForm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const param = {
            data: this.form.cmd,
            channelName: this.form.channel
          }
          axios.post(`${process.env.VUE_APP_HOST}/serialPortServer/sendCmd`, param);
          this.$message.success("发送成功！")
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName){
      this.$refs[formName].resetFields();
    },
    async getData(){
      this.channelOptions = []
      // if(this.$store.state.setting.channelOptionStore.length == 0){
      //   await this.getOptionData();
      // }
      const option = this.$store.state.setting.channelOptionStore;
      option.forEach(item=>{
        this.channelOptions.push({
          label:item[1],
          value:item[1]
        })
      })
    },
    async getOptionData() {
      const res = await axios.get(`${process.env.VUE_APP_HOST}/channel/list`);
      const array = Object.entries(res)
      array.forEach(item=>{
        item[0] = item[0].slice(1);
      })
      this.$store.state.setting.channelOptionStore = array;
    },
  },
};
</script>
<style lang='scss' scoped>
.right_bottom_inner {
  margin-top: -8px;
  display: flex;
  width: 100%;
  height: 100%;
  align-items: center;
}
</style>

<style lang='scss'>
.right_bottom_inner{
  .el-form{
    .el-form-item{
      .el-form-item__label{
        color: white;
      }
    }
  }
}
</style>
